//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.xjrsoft.module.bi.common.tool;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TemplateUtil {
    private static final Pattern pattern = Pattern.compile("\\$\\{([^{}]+)}|\\#\\{([^{}]+)}");

    public TemplateUtil() {
    }

    public static String process(String template, Kv params) {
        Matcher matcher = pattern.matcher(template);
        StringBuffer sb = new StringBuffer();

        while(matcher.find()) {
            String key = matcher.group(1) != null ? matcher.group(1) : matcher.group(2);
            String replacement = params.getStr(key);
            if (replacement == null) {
                throw new IllegalArgumentException("参数中缺少必要的键: " + key);
            }

            matcher.appendReplacement(sb, replacement);
        }

        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String safeProcess(String template, Kv params) {
        Matcher matcher = pattern.matcher(template);
        StringBuffer sb = new StringBuffer();

        while(matcher.find()) {
            String key = matcher.group(1) != null ? matcher.group(1) : matcher.group(2);
            String replacement = params.getStr(key);
            if (replacement != null) {
                matcher.appendReplacement(sb, replacement);
            }
        }

        matcher.appendTail(sb);
        return sb.toString();
    }
}
